/*
 * @Author: LCL 
 * @Date: 2019-03-08 20:36:30
 * @Desc: 玩家管理器
*/
import LadderData from "./LadderData";
import LadderItemControl from "./LadderItemControl";
import { PlayState } from "./LadderItemModel";
import { KingWXUtil } from "../Public/KingWXUtil";


const wx = window['wx']
const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerControl extends cc.Component {

    @property( cc.Sprite )
    playerIcon:cc.Sprite = null

    @property( cc.SpriteFrame )
    leftSpri:cc.SpriteFrame = null

    @property(cc.SpriteFrame)
    rightSpri:cc.SpriteFrame = null;
  
   
    curIndex:number = 0;// 当前在第几层

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    public reset(){
        this.curIndex = 0;
        this.node.position = new cc.Vec2(0,-315);
        LadderData.getInstance().cameraCtr.updateCameraPositon(this.node.position);
    }

    /**
     * 
     * @param type -1往左 1往右
     */
    public uptoNext(type:number){
        let nextLadderNode = LadderData.getInstance().ladderCtr.getTopLadder(this.curIndex+1);
        let nextLadderCtr = nextLadderNode.getComponent(LadderItemControl);

        if(nextLadderCtr.type === type){
            this.curIndex++;
            let endP = new cc.Vec2(nextLadderNode.position.x,nextLadderNode.position.y+65);
            // this.node.position = endP
            this.node.runAction( cc.moveTo( 0.1 ,endP));
            LadderData.getInstance().ladderCtr.addLastFormalLadder();
            LadderData.getInstance().cameraCtr.updateCameraPositon(endP);
            LadderData.getInstance().gameUICtr.upadteScroe();
            if(type == -1){
                this.playerIcon.spriteFrame = this.leftSpri;
            }
            else{
                this.playerIcon.spriteFrame = this.rightSpri;
            }
        }
        else{
            LadderData.getInstance().changePlayState(PlayState.PLAY_END);

            let nextOtherLadNode = LadderData.getInstance().ladderCtr.getTopOtherLadder(this.curIndex+1);
            if(nextOtherLadNode && nextOtherLadNode.getComponent(LadderItemControl).type == type){//跳到装饰楼梯
                KingWXUtil.showShake();
                this.gameOver();
            }
            else{//跳空了
                let endP = new cc.Vec2(nextLadderNode.position.x+type*143,nextLadderNode.position.y+65);
                //this.node.runAction( cc.moveTo( 0.1 ,endP));

                let actionFadeInOut = cc.sequence(
                    cc.spawn(cc.moveTo(0.2, endP),cc.moveTo(0.3, cc.v2(endP.x, -1000))),
                    cc.callFunc(this.gameOver, this)
                )
                this.node.runAction(actionFadeInOut);
                //播放死亡音乐
            }
            
            // this.gameOver();
        }

    }

    public gameOver(){
        console.log("游戏结束");
        LadderData.getInstance().changePlayState(PlayState.PLAY_END);
        LadderData.getInstance().uiCtr.showGameOver();
    }


    // update (dt) {}
}

/*
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
*/
